import "babel-polyfill";
import "classlist-polyfill";
import Vue from "vue";
import axios from "./router/axios";
import VueAxios from "vue-axios";
import App from "./App";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import animate from "animate.css";
import Avue from "@smallwei/avue";
import "@smallwei/avue/lib/index.css";
import AvueFormDesign from "@sscfaith/avue-form-design";
import "./permission"; // 权限
import "./error"; // 日志
import router from "./router/router";
import store from "./store";
import { loadStyle, downBlobFile } from "@/util/util";
import { validatenull } from "@/util/validate";
import * as urls from "@/config/env";
import { iconfontUrl, iconfontVersion } from "@/config/env";
import * as filters from "./filters"; // 全局filter
import "./styles/common.scss";
import basicContainer from "./components/basic-container/main";
// 字典数据组件
import DictResolver from '@/components/DictResolver'
// 字典标签组件
import DictTag from '@/components/DictTag'
import CodeDiff from 'v-code-diff'

// 挂载常用全局方法，import 引入
Vue.prototype.validatenull = validatenull;
Vue.prototype.downBlobFile = downBlobFile;

DictResolver.install()

// 千位符
Vue.directive("thousand", {
  // 被绑定元素插入父节点时调用
  inserted: function(el) {
    // 获取input节点
    if (el.tagName.toLocaleUpperCase() !== "INPUT") {
      el = el.getElementsByTagName("input")[0];
    }
    // 千分位
    el.value = parseFloat(el.value).toLocaleString("zh", {
      minimumFractionDigits: 2,
      maximumFractionDigits: 2
    });

    if(el.value === 'NaN') {
      el.value = '';
    }
    // 聚焦转化为数字格式（去除千分位）
    el.onfocus = e => {
      let a = el.value.replace(/,/g, ""); //去除千分号的','
      el.value = parseFloat(a).toFixed(2);
      if(el.value === 'NaN') {
        el.value = '';
      }
    };
    el.onblur = e => {
      el.value = parseFloat(el.value).toLocaleString("zh", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      });
      if(el.value === 'NaN') {
        el.value = '';
      }
    };
  }
});

// 审批组件
import act from '@/components/act'
act.install()

// 审批组件
import actForm from '@/components/actForm'
actForm.install()

// 表单组件
import form from '@/components/form'
form.install()

// 审计通知书组件
import noticeForm from '@/components/auditTypeView'
noticeForm.install()

window.axios = axios;
Vue.use(VueAxios, axios);

Vue.use(ElementUI, {
  size: "small",
  menuType: "text"
});

Vue.use(Avue, {
  size: "small",
  menuType: "text"
});

Vue.use(CodeDiff);

Vue.use(router);

Vue.use(animate);

Vue.use(AvueFormDesign);

// 注册全局容器
Vue.component("basicContainer", basicContainer);
Vue.component('DictTag', DictTag)

// 加载相关url地址
Object.keys(urls).forEach(key => {
  Vue.prototype[key] = urls[key];
});

//加载过滤器
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key]);
});

// 动态加载阿里云字体库
iconfontVersion.forEach(ele => {
  console.log(iconfontUrl.replace("$key", ele));
  loadStyle(iconfontUrl.replace("$key", ele));
});

Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

//设置主题为 DB2

document.body.className = "theme-iview";
